<?php
/**
 * @author spenceryg
 * @date 2022/1/31
 * @time 3:17 下午
 * @focus love & life
 */

namespace app\admin\controller\sys;

use app\admin\controller\sys\business\AdminAccessBusiness;
use app\admin\controller\sys\business\AdminNodeBusiness;
use app\admin\controller\sys\model\AdminNodeModel;
use app\admin\controller\sys\model\AdminRoleNodeModel;

use app\BaseController;
use think\App;
use think\facade\View;
use think\response\Json;

/**
 * 后台菜单
 * Class AdminNode
 * @package app\admin\controller\sys
 */
class AdminNode extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 菜单列表
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index(): string
    {
        View::assign('data', AdminNodeModel::getTreeTableList());
        return View::fetch(app()->getAppPath() . 'controller/sys/view/admin_node/index.html');
    }

    /**
     * 添加菜单
     * @return string|Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add()
    {
        if (request()->isPost()) {
            $name = input('name', '');
            $href = input('href', '');
            $icon = input('icon', '');
            $pid = input('pid', 0);
            $is_show = input('is_show', 0);
            $sort = input('sort', 0);
            $data = [
                'name' => $name,
                'href' => $href,
                'icon' => $icon,
                'pid' => $pid,
                'is_show' => $is_show,
                'sort' => $sort
            ];
            AdminNodeBusiness::add($data);
            return success('增加菜单成功');
        } else {
            $pid_arr = AdminNodeModel::select();
            View::assign('pid_arr', $pid_arr->toArray());
            $r = [];
            $pid = input('pid', 0);
            $r['pid'] = $pid;
            View::assign('r', $r);
            $nodes = AdminAccessBusiness::getAllPublicAction();
            View::assign('nodes', $nodes);
            return View::fetch(app()->getAppPath() . 'controller/sys/view/admin_node/add.html');
        }
    }


    /**
     * 编辑菜单
     * @return string|Json
     */
    public function editor()
    {
        if (request()->isPost()) {
            $name = input('name', '');
            $href = input('href', '');
            $icon = input('icon', '');
            $pid = input('pid', 0);
            $id = input('id', 0);
            $is_show = input('is_show', 0);
            $sort = input('sort', 0);
            $data = [
                'id' => $id,
                'name' => $name,
                'href' => $href,
                'icon' => $icon,
                'pid' => $pid,
                'is_show' => $is_show,
                'sort' => $sort
            ];
            AdminNodeBusiness::update($data);
            return success('编辑菜单成功');
        } else {
            $id = input('id', 0);
            $pid_arr = AdminNodeModel::select();
            View::assign('pid_arr', $pid_arr->toArray());
            $r = AdminNodeModel::find($id)->getData();
            View::assign('r', $r);
            $nodes = AdminAccessBusiness::getAllPublicAction();
            View::assign('nodes', $nodes);
            return View::fetch(app()->getAppPath() . 'controller/sys/view/admin_node/editor.html');
        }
    }

    /**
     * 删除菜单及其子菜单
     * @return Json
     */
    public function delete(): Json
    {
        try {
            $id = input('id', 0);
            AdminNodeModel::treeDelete($id);
            AdminRoleNodeModel::where('admin_node_id', $id)->delete();
            return success('删除菜单成功！');
        } catch (\Exception $e) {
            return error($e->getMessage());
        }
    }


    /**
     * 设置菜单的显示隐藏
     * @return Json
     */
    public function setShow(): Json
    {
        $id = input('id', 0);
        $is_show = input('is_show', 0);
        $save = [
            'id' => $id,
            'is_show' => $is_show
        ];
        AdminNodeModel::update($save);
        if ($is_show) {
            return success('设置菜单隐藏成功！');
        } else {
            return success('设置菜单显示成功！');
        }
    }

    /**
     * 字段排序
     */
    public function updateSort(): Json
    {
        $id = input("id");
        $sort = input("sort");
        foreach ($id as $key => $vid) {
            $data['id'] = (int)$vid;
            $data['sort'] = (int)$sort[$key];
            AdminNodeModel::update($data);
        }
        return success("排序更改成功");
    }
}
